﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Steamrunner
{
    class OptionsScreen : Screen
    {
        IniFile config = null;

        Texture2D background = null;
        Texture2D title = null;

        Texture2D bar1 = null;
        Texture2D bar2 = null;
        Texture2D bar3 = null;
        Texture2D bar4 = null;
        Texture2D bar5 = null;

        int currentSound;
        int currentMusic;

        List<float> volumes;

        string nextScreen;

        public Rectangle screenBoundaries;

        public OptionsScreen(ScreenManager screenManager, Game game)
            : base(screenManager, game)
        {
        }

        public override void Initialize()
        {
            volumes = new List<float>();
            volumes.Add(0f);
            volumes.Add(0.25f);
            volumes.Add(0.5f);
            volumes.Add(0.75f);
            volumes.Add(1f);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            screenBoundaries = Game.Window.ClientBounds;

            guiManager = new GuiManager(Game);

            Button button = new Button(Game, Spritebatch, "Images\\Menus\\o_music", new Vector2(125f, 300f), "Music");
            guiManager.AddButton(button);

            button = new Button(Game, Spritebatch, "Images\\Menus\\o_sound", new Vector2(125f, 500f), "Sound");
            guiManager.AddButton(button);

            button = new Button(Game, Spritebatch, "Images\\Menus\\o_btmm", new Vector2(screenBoundaries.Width * 0.245f, screenBoundaries.Height * 0.825f), "MenuScreen");
            guiManager.AddButton(button);

            guiManager.Initialize();

            background = Game.Content.Load<Texture2D>(@"Images\Menus\background");
            title = Game.Content.Load<Texture2D>(@"Images\Menus\o_title");

            bar1 = Game.Content.Load<Texture2D>(@"Images\Menus\o_bar1");
            bar2 = Game.Content.Load<Texture2D>(@"Images\Menus\o_bar2");
            bar3 = Game.Content.Load<Texture2D>(@"Images\Menus\o_bar3");
            bar4 = Game.Content.Load<Texture2D>(@"Images\Menus\o_bar4");
            bar5 = Game.Content.Load<Texture2D>(@"Images\Menus\o_bar5");

            config = new IniFile("config.ini");
            config.Parse();
            
            float musicVolume = config.GetValue("Volume", "Music");
            if (musicVolume == volumes[0])
            {
                currentMusic = (int)Enums.VolumeState.NOVOLUME;
            }
            else if (musicVolume == volumes[1])
            {
                currentMusic = (int)Enums.VolumeState.LOW;
            }
            else if (musicVolume == volumes[2])
            {
                currentMusic = (int)Enums.VolumeState.MID;
            }
            else if (musicVolume == volumes[3])
            {
                currentMusic = (int)Enums.VolumeState.HIGH;
            }
            else
            {
                currentMusic = (int)Enums.VolumeState.MAX;
            }

            float soundVolume = config.GetValue("Volume", "Sound");
            if (soundVolume == volumes[0])
            {
                currentSound = (int)Enums.VolumeState.NOVOLUME;
            }
            else if (soundVolume == volumes[1])
            {
                currentSound = (int)Enums.VolumeState.LOW;
            }
            else if (soundVolume == volumes[2])
            {
                currentSound = (int)Enums.VolumeState.MID;
            }
            else if (soundVolume == volumes[3])
            {
                currentSound = (int)Enums.VolumeState.HIGH;
            }
            else
            {
                currentSound = (int)Enums.VolumeState.MAX;
            }
        }

        public override void Update(GameTime gameTime)
        {
            musicManager.Update(gameTime);

            inputManager.Update(gameTime);

            if (inputManager.JustPressed(Keys.W))
            {
                guiManager.JoystickUp();
                musicManager.PlaySound("buttonScrolling");
            }
            else if (inputManager.JustPressed(Keys.S))
            {
                guiManager.JoystickDown();
                musicManager.PlaySound("buttonScrolling");
            }

            if (inputManager.JustPressed(Keys.A) || inputManager.JustPressed(Keys.D))
            {
                nextScreen = guiManager.ButtonClicked();
                musicManager.PlaySound("buttonSelect");

                if (nextScreen == "Music")
                {
                    if (inputManager.JustPressed(Keys.A))
                    {
                        currentMusic--;
                        if (currentMusic < (int)Enums.VolumeState.NOVOLUME)
                        {
                            currentMusic = (int)Enums.VolumeState.NOVOLUME;
                        }
                    }
                    else
                    {
                        currentMusic++;
                        if (currentMusic > (int)Enums.VolumeState.MAX)
                        {
                            currentMusic = (int)Enums.VolumeState.MAX;
                        }
                    }
                    musicManager.ChangeMusicVolume(volumes[currentMusic]);
                }
                else if (nextScreen == "Sound")
                {
                    if (inputManager.JustPressed(Keys.A))
                    {
                        currentSound--;
                        if (currentSound < (int)Enums.VolumeState.NOVOLUME)
                        {
                            currentSound = (int)Enums.VolumeState.NOVOLUME;
                        }
                    }
                    else
                    {
                        currentSound++;
                        if (currentSound > (int)Enums.VolumeState.MAX)
                        {
                            currentSound = (int)Enums.VolumeState.MAX;
                        }
                    }
                    musicManager.ChangeSoundVolume(volumes[currentSound]);
                }
                else
                {
                    config.AddModify("Volume", "Music", volumes[currentMusic]);
                    config.AddModify("Volume", "Sound", volumes[currentSound]);
                    config.Save();
                    screenManager.CurrentScreen = nextScreen;
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Spritebatch.Begin();

            Spritebatch.Draw(background, Vector2.Zero, Color.White);

            Spritebatch.Draw(title, Vector2.Zero, Color.White);

            guiManager.Draw(gameTime);

            Vector2 tempPos = new Vector2((screenBoundaries.Width * 0.5f) - (bar1.Width * 0.5f), 300f);
            switch (currentMusic)
            {
                case 0 :
                    Spritebatch.Draw(bar1, tempPos, Color.White);
                    break;
                case 1 :
                    Spritebatch.Draw(bar2, tempPos, Color.White);
                    break;
                case 2 :
                    Spritebatch.Draw(bar3, tempPos, Color.White);
                    break;
                case 3 :
                    Spritebatch.Draw(bar4, tempPos, Color.White);
                    break;
                case 4 :
                    Spritebatch.Draw(bar5, tempPos, Color.White);
                    break;
            }

            tempPos.Y += 200f;
            switch (currentSound)
            {
                case 0:
                    Spritebatch.Draw(bar1, tempPos, Color.White);
                    break;
                case 1:
                    Spritebatch.Draw(bar2, tempPos, Color.White);
                    break;
                case 2:
                    Spritebatch.Draw(bar3, tempPos, Color.White);
                    break;
                case 3:
                    Spritebatch.Draw(bar4, tempPos, Color.White);
                    break;
                case 4:
                    Spritebatch.Draw(bar5, tempPos, Color.White);
                    break;
            }

            Spritebatch.End();

            base.Draw(gameTime);
        }

        public override string GetNextScreen()
        {
            return nextScreen;
        }
    }
}
